﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using ActivityTracker.Library;
using Common.Library;


namespace ActivityTracker
{
    public partial class AddPastActivityHistory : PhoneApplicationPage
    {
        #region Global

        Actvity objActivity = new Actvity();
        History objHistory = new History();
        string actId = "";

        #endregion

        #region constructor

        public AddPastActivityHistory()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (NavigationContext.QueryString.TryGetValue("actId", out actId))
                    tbxActivityname.Text = objActivity.GetActivityName(actId);
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In AddPastActivityHistory file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbxActivityname.Text != "")
                {
                    DateTime startdate = Convert.ToDateTime(dpicDate.Value);
                    DateTime startTime = Convert.ToDateTime(dpicStartTime.Value);
                    DateTime EndTime = Convert.ToDateTime(dpicEndTime.Value);
                    objHistory.SaveActivity(actId, tbxActivityname.Text, startdate.ToShortDateString(), startTime.ToShortTimeString());
                    objHistory.SaveActivityHistory(SettingsHelper.getStringValue("hisId"), EndTime.ToShortTimeString(), Convert.ToInt32(tblkTotalDuration.Text));
                    objHistory.SaveTotalduration(actId, tbxActivityname.Text, Convert.ToInt32(tblkTotalDuration.Text), startdate.ToShortDateString());
                    //string historyId = "";
                    //historyId = SaveData.SaveActivity(ActivityId, txtboxActivityname.Text, startdate.ToShortDateString(), startTime.ToShortTimeString());
                    //SaveData.SaveActivityHistory(historyId, EndTime.ToShortTimeString(), txtblkTotalDuration.Text);
                    //SaveData.saveTotalduration(ActivityId, txtboxActivityname.Text, txtblkTotalDuration.Text, startdate.ToShortDateString());
                    NavigationService.Navigate(new Uri("/Views/Activities/MyActivities.xaml", UriKind.Relative));
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarSave_Click Method In AddPastActivityHistory file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void dpicStartTime_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            try
            {
                TimeSpan st;
                st = Convert.ToDateTime(dpicEndTime.Value).Subtract(Convert.ToDateTime(dpicStartTime.Value));
                int TotalMinutes = 0;
                if (st.Hours > 0)
                {
                    TotalMinutes = TotalMinutes + (st.Hours * 60);
                }
                if (st.Minutes > 0)
                {
                    TotalMinutes = TotalMinutes + st.Minutes;
                }
                if (st.Seconds > 0)
                {
                    TotalMinutes = TotalMinutes + (st.Seconds / 60) + 1;
                }
                tblkTotalDuration.Text = TotalMinutes.ToString();
            }
            catch (Exception ex)
            {
                string mess = "Exception in dpicStartTime_ValueChanged Method In AddPastActivityHistory file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void dpicEndTime_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            try
            {
                TimeSpan st;
                st = Convert.ToDateTime(dpicEndTime.Value).Subtract(Convert.ToDateTime(dpicStartTime.Value));
                int TotalMinutes = 0;
                if (st.Hours > 0)
                {
                    TotalMinutes = TotalMinutes + (st.Hours * 60);
                }
                if (st.Minutes > 0)
                {
                    TotalMinutes = TotalMinutes + st.Minutes;
                }
                if (st.Seconds > 0)
                {
                    TotalMinutes = TotalMinutes + (st.Seconds / 60) + 1;
                }
                tblkTotalDuration.Text = TotalMinutes.ToString();
            }
            catch (Exception ex)
            {
                string mess = "Exception in dpicEndTime_ValueChanged Method In AddPastActivityHistory file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        #endregion
    }
}